import {
  formatMenuIcon,
  handleCustomMenu,
  handleSystemMenu
} from '@/utils/menu'

import {
  handlePerspective
} from '@/utils/krpano'

import { isMobile } from '@/utils'
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      isShow: false,
      actions: [],
      sheets: false,
      menuDataList: []
    }
  },
  mounted() {
    setTimeout(() => {
      this.isShow = true
    }, 3 * 1000)

    // 监听utils/menu 菜单处理
    this.showPerspective()
  },
  computed: {
    ...mapGetters([
      'panoInfo',
      'themeId',
      'menuData',
      'visibleBarrage',
      'getBtMenu'
    ]),
    getThemeId() {
      if(this.themeId) return this.themeId
    },
    getMenuStyleObj() {
      return this.visibleBarrage ? true : false
    },
    getMenuStatus() {
      if (this.panoInfo && this.menuData) {
        return true
      }
    },
    getNavMenu() {
      let menuList = []
      if(this.menuData) {
        let navMenu = this.menuData.navmenu
        if(navMenu.length > 0) {
          navMenu.map(item => {
            if(item.children.length > 0) {
              // 让子菜单排序在父级元素下面
              item.icon = formatMenuIcon(item.type_id)
              menuList.push(item)
              item.children.map(child => {
                child.icon = formatMenuIcon(child.type_id)
              })
            } else {
              item.icon = formatMenuIcon(item.type_id)
              menuList.push(item)
            }
          })
        }
      }
      return menuList
    },
    getBottomMenu() {
      let menuList = []
      if(this.menuData){
        let btMenu = this.menuData.bottommenu
        if(btMenu.length) {
          btMenu.map(item => {
            item.icon = formatMenuIcon(item.type_id)
            menuList.push(item)
          })
        }
      }
      return menuList
    },
    getAllMenu() {
      let list = []
      let navMenu = this.getNavMenu
      let btMenu = this.getBottomMenu

      list = list.concat(navMenu, btMenu)
      return list
    }
  },
  methods: {
    handleSheet() {
      this.sheets = true
      this.changeZindex(99)
    },
    sheetCancel() {
      this.sheets = false
      this.changeZindex(10)
    },
    showScene() {
      this.$vgo.emit('scene:toggle')
      // this.changeZindex(99)
    },
    // 自定义底部菜单
    handleBtMenu(item) {
      this.sheets = false
      this.changeZindex(10)
      handleCustomMenu(item)
    },
    // 系统菜单
    handleSytemMenuItem(item, index) {
      // console.log(item, 4777)
      if (item.type_id === 'bgmusic') {
        if (item.isActive) {
          window.sessionStorage.setItem('ISUSER', '1')
        } else {
          window.sessionStorage.setItem('ISUSER', '0')
        }
      }
      this.sheets = false
      this.changeZindex(10)
      item.isActive = !item.isActive
      this.$set(item, 'isActive', item.isActive)
      if(item.type_id == 'view' || item.type_id == '113') {
        this.handlePerspective()
      }
      if(item.type_id == 'say') {
        this.$krp.onShowSay()
      }
      handleSystemMenu(item, index)
    },
    showPerspective() {
      this.$vgo.on('showPerspectiveMenu', () => {
        this.handlePerspective()
      })
    },
    handlePerspective() {
      let perspectData = this.$CONFIG.perspectiveMenu
      this.formatActions(perspectData, this.handlePerspect)
      this.handleSheet()
    },
    handlePerspect(item) {
      handlePerspective(item.type)
      this.sheetCancel()
    },
    changeZindex(num) {
      if(isMobile()) {
        if (this.$refs.footWrap && this.$refs.footWrap.style) {
          this.$refs.footWrap.style.zIndex = num
        }
      }
    },
    formatActions(menu, callback) {
      if(!menu) return
      this.actions = []
      // console.log(menu)
      menu.map(item => {
        item.name = item.name ? item.name : item.title
        item.callback = callback

        if(item.children && item.children.length > 0) {
          item.children.map(child => {
            child.name = child.name ? child.name : child.title
            child.callback = callback
            this.actions.push(child)
          })
        }
        this.actions.push(item)
      })
    },
    handleDragPrevent(e) {
      e.preventDefault()
    }
  }
}
